<?php

/**
 * Subclass for representing a row from the 'seo' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Seo extends BaseSeo
{
    /**
     * 
     * returns obj id, by seo url, 0 if seo with this url doesnt exists
     * @param string $slug 
     */
    public static function getObjIdBySlug($slug)
    {
        $c = new Criteria();
        $c->add(SeoPeer::URL, $slug);
        $obj = SeoPeer::doSelectOne($c);
        $id = 0;
        if($obj)
        {
            return $id = $obj->getObjId();
        }
        return $id;
    }
	/**
     * 
     * returns slug (seo url) or empty string, if seo not found
     * @param int $objId
     * @param string $objType
     * @param string $objAction
     * @param string $culture
     */
    public static function getSlugByObj($objId, $objType, $objAction, $culture)
    {
        /* @var $seo Seo */
        $c = new Criteria();
        $c->add(SeoPeer::OBJ_TYPE, $objType);
        $c->add(SeoPeer::OBJ_ACTION, $objAction);
        $c->add(SeoPeer::OBJ_ID, $objId);
        $c->add(SeoPeer::CULTURE, $culture);
        $seo = SeoPeer::doSelectOne($c);
        $slug = '';
        if( $seo )
        {
            $slug = $seo->getUrl();
        }
        return $slug;
    }
    public static function toSeo($objType, $objAction, $objId, $name, $url, $culture = false, $unique = true) 
    {
        if( $objType && $objAction && $objId && $name) 
        {
            $c = new Criteria();
            $c->add(SeoPeer::OBJ_TYPE, $objType);
            $c->add(SeoPeer::OBJ_ACTION, $objAction);
            $c->add(SeoPeer::OBJ_ID, $objId);
            if($culture)
            {
                $c->add(SeoPeer::CULTURE, $culture);
            }
            $seo = SeoPeer::doSelectOne($c);
            if( !$seo ) 
            { 
                $newSeo = new Seo();
                if( $url ) 
                {
                    $newSeo->setUrl($url);
                }
                else 
                {
                    $newSeo->setUrl(Seo::slugify($name));
                }
                if($culture)
                    $newSeo->setCulture($culture);
                $newSeo->setObjType($objType);
                $newSeo->setObjAction($objAction);
                $newSeo->setObjId($objId);
                $newSeo->save();
                return $newSeo;
            }
            else 
            {
                if( $url ) 
                {
                    $seo->setUrl($url);
                	$seo->save();
                }
                else 
                {
                    $seo->setUrl(Seo::slugify($name));
                	$seo->save();
                }
                return $seo;
            }
        }
    }

    public static function slugify($text) 
    {
        if( $text ) 
        {
            // replace non letter or digits by -
            $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
            
            // trim
            $text = trim($text, '-');
            
            $text = str_replace(array('ą', 'č', 'ę', 'ė', 'į', 'š', 'ų', 'ū', 'ž', 'Ą', 'Č', 'Ę', 'Ė', 'Į', 'Š', 'Ų', 'Ū', 'Ž'), array('a', 'c', 'e', 'e', 'i', 's', 'u', 'u', 'z', 'A', 'C', 'E', 'E', 'I', 'S', 'U', 'U', 'Z'), $text);
            
            // transliterate
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
            
            // lowercase
            $text = strtolower($text);
            
            // remove unwanted characters
            $text = preg_replace('~[^-\w]+~', '', $text);
            
            return $text;
        }
        else 
        {
            return '#';
        }
    }
}
